package com.aaa.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "EncodeFilter",urlPatterns = {"/*"})
public class EncodeFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
     // System.out.println("这是一个字符编码格式的过滤器");
    }

    public void destroy() {
      System.out.println("字符编码格式过滤器的销毁");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {

       // System.out.println("这是一个字符编码格式的过滤器");
      // post请求的时候  req.set
      HttpServletRequest req =(HttpServletRequest) request;
      HttpServletResponse resp=(HttpServletResponse)response;
      // req请求的时候防止乱码
      req.setCharacterEncoding("utf-8");
      // 对页面做出响应的时候防止乱码
        //resp.setCharacterEncoding("utf8");
        resp.setContentType("application/json;charset=utf8");

      chain.doFilter(request, response);
        //System.out.println("_________________________");
    }
}
